<?php 

Class adController extends appController
{
	
	public function index()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$ad_list=Model('ad')->ad_list($page);
		$this->assign('ur_here','>广告管理');
		//$this->assign('search_action','index.php?g=admin&m=ad&a=search_group');
		$this->assign('ad_list',$ad_list['data']);
		$this->assign('user_list_page',$ad_list['pagelink']);
		$this->display('index.html');
	}
	public function del_group()
	{
// 		$this->admin_priv('delimage');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('ad')->del_group($v);
			}
		}
		else
		{
			$result=Model('ad')->del_group($id);
		}
		print_r(json_encode($result));
	}
	public function del_content()
	{
// 		$this->admin_priv('delimg');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('ad')->del_img($v);
			}
		}
		else
		{
			$result=Model('ad')->del_img($id);
		}
		print_r(json_encode($result));
	}
	public function ordering()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$val=isset($_POST['val'])?intval($_POST['val']):'';
		$result=Model('ad')->ordering($val,$id);
		print_r(json_encode($result));
	}
	public function content_list()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$flash_id=isset($_GET['bid'])?intval($_GET['bid']):'';
		$img_list=Model('ad')->content_list($page,$flash_id);
		$this->assign('flash_id',$flash_id);
		$this->assign('ad_list',$img_list['data']);
		$this->assign('user_list_page',$img_list['pagelink']);
		if($flash_id=="20"){//特殊广告页面
			$this->display('special_content_list.html');
		}else{
			$this->display('content_list.html');
		}
	}
	public function edit_group()
	{
// 		$this->admin_priv('editgroup');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$ad_group=Model('ad')->getGroup($id);
		$this->assign('ad_group',$ad_group);
		$this->assign('id',$id);
		$this->assign('action','update_group');
		$this->display('group.html');
	}
	public function update_group()
	{
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['description']=isset($_POST['description'])?$_POST['description']:'';
		$info['code']=isset($_POST['code'])?$_POST['code']:'';
		$info['weight']=isset($_POST['weight'])?$_POST['weight']:'';
		$info['height']=isset($_POST['height'])?$_POST['height']:'';
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if(Model('ad')->check_code($info['code'],$id))
		{
			if(Model('ad')->update_group($info,$id))
			{
				$data['error']=0;
				$data['content']=Lang('SUCCESS_EDIT');
				$this->showMsg($data);
			}
			else
			{
				$data['content']=Lang('INVALID_OPERATION');
				$this->showMsg($data);
			}
		}
		else
		{
			$data['content']=Lang('CODE_REPEATING');
			$this->showMsg($data);
		}
		
		
	}
	public function add_group()
	{
// 		$this->admin_priv('addgroup');
		$this->assign('ur_here','>添加广告位');
		$this->assign('action','create_group');
		$this->display('group.html');
	}
	
	public function create_group()
	{
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['description']=isset($_POST['description'])?$_POST['description']:'';
		$info['code']=isset($_POST['code'])?$_POST['code']:'';
		$info['weight']=isset($_POST['weight'])?$_POST['weight']:'';
		$info['height']=isset($_POST['height'])?$_POST['height']:'';
		$result=array('error'=>1,'content'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if(Model('ad')->check_code($info['code']))
		{
		  if($ad_id=Model('ad')->insert($info,'block'))
		  {
		  	   $result['error']=0;
			  $result['content']=Lang('SUCCESS_ADD');
		  }
		  else
		  {
				$result['content']=Lang('INVALID_OPERATION');
		  }
		}
		else 
		{
			$result['content']=Lang('CODE_REPEATING');
		}
		$this->showMsg($result);
	}
	public function add_content()
	{
// 		$this->admin_priv('addimage');
		$flash_id=isset($_GET['bid'])?intval($_GET['bid']):'';
		$this->assign('ur_here','><a href="index.php?g=admin&m=ad&a=images_list&flash_id='.$flash_id.'">广告管理</a>>添加图片');
		$this->assign('bid',$flash_id);
		$this->assign('action','sumbit_content');
		if($flash_id=="20"){//特殊广告页面
			$count = Model("ad")->get_ad_size();
			$this->assign("adCount", $count);
			$this->display('special_content.html');
		}else{
			$this->display('content.html');
		}
		
	}
	public function edit_content()
	{
// 		$this->admin_priv('editimage');
		$this->assign('ur_here','>修改图片');
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$ad_img=Model('ad')->getImg($id);
		$this->assign('ad',$ad_img);
		$this->assign('bid',$ad_img['block_id']);
		$this->assign('id',$id);
		$this->assign('action','update_content');
		if($ad_img['block_id']=="20"){//特殊广告页面
			$count = Model("ad")->get_ad_size();
			$this->assign("adCount", $count);
			$this->display('special_content.html');
		}else{
			$this->display('content.html');
		}
	}
	public function update_content()
	{
		$en_title=isset($_POST['en_title'])?$_POST['en_title']:'';
		$info['title']=isset($_POST['title'])?CH_SC.$_POST['title'].DIG_EC.EN_SC.$en_title.DIG_EC:'';
		$en_des=isset($_POST['en_description'])?$_POST['en_description']:'';
		$info['description']=isset($_POST['description'])?CH_SC.$_POST['description'].DIG_EC.EN_SC.$en_des.DIG_EC:'';
		$info['url']=isset($_POST['url'])?$_POST['url']:'';
		$info['ordering']=isset($_POST['ordering'])?$_POST['ordering']:'';
		$info['pic_path']=isset($_POST['pic_path'])?$_POST['pic_path']:'';
		$info['ad_place']=isset($_POST['ad_place'])?intval($_POST['ad_place']):0;
		$info['created']=$_POST['created']==''?$_SERVER['REQUEST_TIME']:strtotime($_POST['created']);
		$info['ad_starttime']=isset($_POST['ad_starttime'])?strtotime($_POST['ad_starttime']):'';
		$info['ad_endtime']=isset($_POST['ad_endtime'])?strtotime($_POST['ad_endtime']):'';
		$id=isset($_POST['id'])?$_POST['id']:'';
		$data=array('error'=>1,'content'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
			$result=Model('ad')->update($info,array('table'=>'block_content','id'=>$id));
			
			if($result)
		   {
		   	$data['error']=0;
			$data['content']=Lang('SUCCESS_EDIT');
			
		   }
		   else
		   {
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		   }
		    $this->showMsg($data);
		
	}
	
	public function sumbit_content()
	{
		$en_title=isset($_POST['en_title'])?$_POST['en_title']:'';
		$info['title']=isset($_POST['title'])?CH_SC.$_POST['title'].DIG_EC.EN_SC.$en_title.DIG_EC:'';
		$en_des=isset($_POST['en_description'])?$_POST['en_description']:'';
		$info['description']=isset($_POST['description'])?CH_SC.$_POST['description'].DIG_EC.EN_SC.$en_des.DIG_EC:'';
		$info['url']=isset($_POST['url'])?$_POST['url']:'';
		$info['ordering']=isset($_POST['ordering'])?$_POST['ordering']:0;
		$info['block_id']=isset($_POST['block_id'])?$_POST['block_id']:'';
		$info['pic_path']=isset($_POST['pic_path'])?$_POST['pic_path']:'';
		$info['ad_place']=isset($_POST['ad_place'])?intval($_POST['ad_place']):0;
		$info['ad_starttime']=isset($_POST['ad_starttime'])?strtotime($_POST['ad_starttime']):'';
		$info['ad_endtime']=isset($_POST['ad_endtime'])?strtotime($_POST['ad_endtime']):'';
		$info['created']=$_POST['created']==''?$_SERVER['REQUEST_TIME']:strtotime($_POST['created']);
		$data=array('error'=>0,'content'=>'','url'=>'');
		if($_POST['title']=='')
		{
			$data['error']=1;
			$data['content']=Lang('标题不能为空');
			$this->showMsg($data);
		}
		if($_POST['pic_path'])
		{
		   $result=Model('ad')->add_images($info);
		   
			if($result['error']==0)
		   {
			$data['content']=Lang('SUCCESS_ADD');
			$data['url']="index.php?g=admin&m=ad&a=images_list&flash_id=".$info['flash_id'];
			
		   }
		   else
		   {
		   	
			$data['content']=$result['content'];
			//$data['url']=$_SERVER['REQUEST_URI'];
		   }
		    $this->showMsg($data);
		}
		else 
		{
			$data['content']=Lang('INVALID_OPERATION');
			$this->showMsg($data);
		}
	}
}